import config from "./config"
import {showToast} from '@/utils/method.js'
class Instance {
	request(url, method = 'POST', data, loading = true, header = {}) {
		if (loading) {
			// 页面加载数据中等待的图标
			uni.showLoading({
				title: '加载中',
				mask: true
			});
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.BASE_URl + url,
				method,
				data,
				header: {Token:uni.getStorageSync('token')},
				success: res => {
					if(res.data.code===0 && url != 'login/login'){
						return	 setTimeout(() => {
									showToast(res.data.msg)
							},500);
					}else if (res.data.code===4){
						  showToast(res.data.msg)
						return	navTo('/pagesB/wxlogin/wxlogin')
					}
					
					resolve(res)
					
				},
				fail: err => {
					reject(err)
				},
				complete: () => {
					uni.hideLoading();
				}
			})
		})
	}
	get(url, params, loading = false, header) {
		//判断是否有网络
		uni.getNetworkType({
			success: function (res) {
				if (res.networkType === "none") {
					uni.showToast({
						icon: "none",
						title: "网络异常，请检测网络配置!",
						duration: 2000,
					})
				}
			},
		})
		return this.request(url, "GET", params, loading, header)
	}
	post(url, data, loading = false, header) {
		//判断是否有网络
		uni.getNetworkType({
			success: function (res) {
				if (res.networkType === "none") {
					uni.showToast({
						icon: "none",
						title: "网络异常，请检测网络配置!",
						duration: 2000,
					})
				}
			},
		})
		return this.request(url, "POST", data, loading, header)
	}
}

const instance = new Instance()

export default instance